package com.hutao.manage.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {

	public static boolean createOrEditFile(String data,String fileDir,String fileName) throws IOException{
		File destFile = new File(fileDir,fileName);
		if(!destFile.exists()){
			File destDir = new File(fileDir);
			if(!destDir.exists()){
				destDir.mkdirs();
			}
			destFile.createNewFile();
		}
		
		FileOutputStream fileOutputStream=new FileOutputStream(destFile);  
        OutputStreamWriter outputWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");  
        outputWriter.write(data);
         
        outputWriter.close();  
        fileOutputStream.close();  

       
		return true;
	}
	
	public static Map<String, Object> readLineFile(String filePath, String fileName) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		try {
			File destFile = new File(filePath, fileName);
			String jsonStr = null;
			if (destFile.exists()) {
				// FileReader fr = new FileReader(destFile);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath + fileName), "UTF-8"));
				jsonStr = br.readLine();
				jsonMap = JsonUtil.jsonToPojo(jsonStr, Map.class);

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonMap;
	}
	
	/**
	 * 判断文件是否存在
	 * @param filePath
	 * @return
	 */
	public  static boolean fileIsExists(String filePath) {
		return  new File(filePath).exists()?true:false;
	}
	
	
	public static void main(String[] args) {
		System.out.println(readLineFile("D:\\publics\\hospital\\visit\\json\\03\\03\\4e\\96\\","03034e966b4c43a4856d1e3694637e57.json"));
		
	}
	
	
}
